#encoding=utf-8

import datetime

from django.db import models
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
from django.forms import ModelForm, PasswordInput
from django.contrib.admin.widgets import *

from gm_site import settings


UPLOAD_ROOT = settings.UPLOAD_ROOT

#model 中有的列，保证数据库要有，否则会报错。反之可以允许数据库的列比model的多

#游戏服务器表,增改，不可删除，由andy提供
class ServerInfo(models.Model):
    pf = models.CharField("服号", max_length=32,unique=True)
    name = models.CharField("名称", max_length=32)
    host = models.CharField("地址", max_length=32)
    port = models.IntegerField("端口")
    create_time = models.DateTimeField("创建时间",auto_now_add=True)
    STATUS_CHOICES=(
        (0,u'关闭'),

        (1,u'开放')
    )
    status = models.IntegerField('状态',default=0,choices=STATUS_CHOICES)
    server_id=models.IntegerField('服务器ID',default=0)


    def __str__(self):
        return self.pf

    class Meta:
        verbose_name = '服务器信息'
        verbose_name_plural = '服务器信息列表'

#白名单，就是开服前，某些号可以优先进去玩
class WhiteList(models.Model):
    uids = models.CharField("玩家帐号", max_length=200,help_text='多个用逗号隔开')#多个用逗号隔开
    #server=models.ForeignKey(ServerInfo,to_field='pf',verbose_name="服"),
    server= models.ManyToManyField(ServerInfo,verbose_name="服")
    start_time = models.DateTimeField("开始时间")
    end_time = models.DateTimeField("结束时间")
    create_date = models.DateTimeField("创建时间",auto_now_add=True)

    def __str__(self):
        return self.uids

    class Meta:
        verbose_name = '白名单信息'
        verbose_name_plural = '白名单列表'

#黑名单，可操作禁言，封号等操作
class BlackList(models.Model):
    uids = models.CharField("玩家帐号", max_length=200,help_text='多个用逗号隔开')#多个用逗号隔开
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    start_time = models.DateTimeField("开始时间",default='',blank=True,null=True)
    end_time = models.DateTimeField("结束时间",default='',blank=True,null=True)
    STATUS_CHOICES = (
         (0, u'封号'),
        (1, u'禁言'),
    )
    status = models.IntegerField("状态",default=0,choices=STATUS_CHOICES)
    reason = models.TextField("原因", max_length=200,default='',blank=True)
    operid = models.CharField("操作员", max_length=32,default='',blank=True)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)

    def __str__(self):
        return self.uids

    class Meta:
        verbose_name = '黑名单信息'
        verbose_name_plural = '黑名单列表'

#公告表，跑马灯
class NewInfo(models.Model):
    title = models.CharField("标题", max_length=32)
    content = models.TextField("内容", max_length=200)
    start_time = models.DateTimeField("开始时间")
    end_time = models.DateTimeField("结束时间")
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    orderid = models.IntegerField("优先级",default=0)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)
    EFFECT_CHOICES=(
        (0,u'否'),
        (1,u'是'),
    )
    effect=models.IntegerField('是否滚动',default=0,choices=EFFECT_CHOICES)
    times=models.IntegerField('滚动时间',default=0)
    serial=models.IntegerField('标识',default=0)
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '公告信息'
        verbose_name_plural = '公告信息列表'

#聊天框表，
class ChatInfo(models.Model):
    title = models.CharField("标题", max_length=32)
    content = models.TextField("内容", max_length=200)
    start_time = models.DateTimeField("开始时间")
    end_time = models.DateTimeField("结束时间")
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    intervals = models.IntegerField("间隔",default=0)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '聊天框'
        verbose_name_plural = '聊天框列表'

#邮件，
class EmailInfo(models.Model):
    uids = models.CharField("玩家帐号", max_length=200,help_text='多个用逗号隔开',default='')#多个用逗号隔开
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    title = models.CharField("标题", max_length=32)
    content = models.TextField("内容", max_length=200)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)
    propid = models.CharField('道具id',max_length=200,default='')
    number = models.CharField('数量', max_length=200, default='')
    STATE_CHOICES=(
        (1,u'全服'),
        (2,u'特定玩家'),
        (3,u'特定等级范围'),
    )
    state =models.IntegerField('道具范围',default=1,choices=STATE_CHOICES)
    arena = models.CharField('等级范围',max_length=500,default='')

    def __str__(self):
        return self.uids

    class Meta:
        verbose_name = '邮件信息'
        verbose_name_plural = '邮件列表'

#礼包，
class PackInfo(models.Model):
    uids = models.CharField("玩家帐号", max_length=200,help_text='多个用逗号隔开')#多个用逗号隔开
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    packid = models.CharField("包id", max_length=32)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)

    def __str__(self):
        return self.uids

    class Meta:
        verbose_name = '礼包信息'
        verbose_name_plural = '礼包列表'

#道具，
class PropInfo(models.Model):
    uids = models.CharField("玩家帐号", max_length=200,help_text='多个用逗号隔开')#多个用逗号隔开
    server=models.ManyToManyField(ServerInfo,verbose_name="服")
    propid = models.CharField("道具id", max_length=32,default='')
    amount = models.IntegerField("数量",default=0)
    create_date = models.DateTimeField("创建时间",auto_now_add=True)
    typeid=models.IntegerField('类型',default=None)
    silver=models.IntegerField('游戏币',default=None)
    gold = models.IntegerField('金币',default=None)
    stage=models.IntegerField('关卡',default=None)
    task=models.IntegerField('任务',default=None)

    def __str__(self):
        return self.uids

    class Meta:
        verbose_name = '道具信息'
        verbose_name_plural = '道具列表'

#登录日志，
class LogLogin(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    ip = models.CharField("IP", max_length=20)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("服", max_length=20,db_index=True)
    create_time = models.DateTimeField("时间")

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '登录日志'
        verbose_name_plural = '登录日志'


#在线日志，
class LogOnline(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    create_time = models.DateTimeField("时间")

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '在线日志'
        verbose_name_plural = '在线日志'


#用户表，
class UserInfo(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    account=models.CharField("帐号", max_length=40)
    username=models.CharField("名称", max_length=40)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    create_time = models.DateTimeField("时间")
    recently_online=models.DateTimeField('最近在线')

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = '用户列表'

#引导日志，
class LogLead(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    stage= models.CharField("阶段", max_length=10)
    step= models.IntegerField("步骤",default=0)
    create_date = models.DateField("日期",db_index=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '引导日志'
        verbose_name_plural = '引导日志'


#消费日志，
class LogConsumer(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    unit= models.CharField("单位", max_length=10)
    amt= models.IntegerField("数量",default=0)
    create_time = models.DateTimeField("时间",db_index=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '消费日志'
        verbose_name_plural = '消费日志'

#充值日志，
class LogRecharge(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    orderid= models.CharField("订单号", max_length=40)
    order_id= models.CharField("订单号", max_length=40)
    item= models.CharField("物品", max_length=10)
    money= models.IntegerField("金额",default=0)
    isPay=models.BooleanField("状态",default=True)
    create_time = models.DateTimeField("时间",db_index=True)
    account = models.CharField('账号',default='',max_length=200,blank=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '充值日志'
        verbose_name_plural = '充值日志'

#购买物品日志，
class LogBuyItem(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    buy_id= models.CharField("道具id", max_length=10,db_index=True)
    num= models.IntegerField("数量",default=0)
    amt= models.IntegerField("花费",default=0)
    reason= models.CharField("来源", max_length=20)
    create_time = models.DateTimeField("时间",db_index=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '购买日志'
        verbose_name_plural = '购买日志'

#阶段排行榜，
class RankStage(models.Model):
    uid = models.CharField("uid", max_length=40,db_index=True)
    server = models.CharField("服", max_length=20,db_index=True)
    platform= models.CharField("平台", max_length=20,db_index=True)
    reason= models.CharField("来源", max_length=20)
    name= models.CharField("呢称", max_length=10)
    rankid= models.IntegerField("名次",default=0)
    score= models.IntegerField("分数",default=0)
    stageid= models.IntegerField("阶段")
    create_time = models.DateTimeField("时间",db_index=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '购买日志'
        verbose_name_plural = '购买日志'

#------------------------------------------
#礼物码
class GiftCode(models.Model):
    serial=models.CharField('批次ID',max_length=30,blank=True,null=True)
    server_id=models.CharField('区服标识',max_length=2048,blank=True,null=True)
    endtime=models.CharField('过期时间',max_length=60,blank=True,null=True)
    items=models.CharField('发放物品',max_length=100,blank=True,null=True)
    gift_type=models.IntegerField('礼物类型',blank=True,null=True)
    is_plat =models.IntegerField('是否平台礼包',default=1)
    origin = models.CharField('来源',max_length=32,default='',blank=True,null=True)
    activeid=models.IntegerField('活动ID',blank=True,null=True)
    number= models.IntegerField('数量',blank=True,null=True)


    def __str__(self):
        return self.server_id

    class Meta:
        verbose_name = '礼物码日志'
        verbose_name_plural = '礼物码日志'





#登录公告记录
class LoginNews(models.Model):
    content = models.TextField('修改内容',max_length=500,default='',blank=True)
    bak_use = models.CharField('备用',max_length=200,default='',blank=True)
    change_time = models.DateTimeField('修改时间',auto_now_add=True)

    def __str__(self):
        return str(self.id)

    class Meta:
        verbose_name = '登录公告记录'
        verbose_name_plural = '登录公告记录'


#礼物码
class Gift_Code(models.Model):
    serial_no = models.CharField('批次ID',max_length=60,default='',blank=True)
    is_plat = models.IntegerField('是否平台礼包',default=1,blank=True)
    server_code=models.CharField('区服ID',max_length=200,blank=True)
    expired_utc=models.CharField('过期时间',max_length=200,blank=True)
    items=models.CharField('发放物品',max_length=200,blank=True)
    gift_type=models.IntegerField('礼物码类型',blank=True)
    activity_id=models.IntegerField('活动ID',blank=True)
    gift_num=models.IntegerField('数量',default=0,blank=True)
    code =models.CharField('礼物码',max_length=200,blank=True)
    create_time=models.DateTimeField('生成时间',auto_now_add=True)

    def __str__(self):
        return self.serial_no

    class Meta:
        verbose_name = '礼物码'
        verbose_name_plural = '礼物码'




#后台充值操作记录
class Recharge_info(models.Model):
    uid = models.CharField('角色ID',max_length=200,default='',blank=True)
    charge_moeny = models.CharField('充值金额',default='',max_length=100,blank=True)
    charge_id= models.IntegerField('充值ID',default=0,blank=True)
    create_time = models.DateTimeField('充值时间',auto_now_add=True)

    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '后台充值操作记录'
        verbose_name_plural = '后台充值操作记录'



#server黑白名单表
class Black_White(models.Model):
    userid=models.CharField('玩家账号',max_length=200,default='')
    server=models.CharField('服号',default='',max_length=100,blank=True)
    start_time = models.DateTimeField("开始时间", default='', blank=True, null=True)
    end_time = models.DateTimeField("结束时间", default='', blank=True, null=True)
    operid = models.CharField('操作者',default='',max_length=100,blank=True)
    create_time = models.DateTimeField('创建时间',auto_now_add=True)
    types=models.IntegerField('类型',blank=True,null=True)#0为白名单,1为黑
    f_gm = models.CharField('预留',default='',max_length=100,blank=True)

    def __str__(self):
        return self.userid

    class Meta:
        verbose_name = '服务器黑白名单表'
        verbose_name_plural = '服务器黑白名单表'



#邮件回复记录表
class Reply_Record(models.Model):
    uid=models.CharField('玩家UID',max_length=200,default='')
    nickname=models.CharField('昵称',max_length=200,default='',blank=True)
    content=models.TextField('内容',max_length=255,default='',blank=True)
    is_who=models.IntegerField('内容所属',blank=True,null=True)#0为玩家，1为GM管理者
    is_reply=models.IntegerField('状态',default=0,blank=True)#0为新，1为已读，2为已解决
    oper=models.CharField('操作者',default='',max_length=100,blank=True,null=True)
    create_time=models.DateTimeField('创建时间',auto_now_add=True)
    serverid=models.CharField('服务器',blank=True,null=True,default='',max_length=60)
    title=models.CharField('标题',max_length=100,blank=True,null=True,default='')


    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '回复记录表'
        verbose_name_plural = '回复记录表'


#加载流失数据
class Loading(models.Model):
    steps=models.CharField('步骤',max_length=100,default='',blank=True)
    number=models.IntegerField('数量',default=0,blank=True)
    date_time=models.DateField('时间',auto_now_add=True)

    def __str__(self):
        return self.steps

    class Meta:
        verbose_name = '加载流失数据表'
        verbose_name_plural = '加载流失数据表'



#在线统计数据 十分钟统计一条
class Report_Data(models.Model):
    online_count=models.IntegerField('在线人数',default=0,null=True,blank=True)
    create_date=models.DateField('日期')
    create_time=models.DateTimeField('生成时间')
    which_from=models.CharField('区服或渠道',max_length=100,default='',blank=True,null=True)


    def __str__(self):
        return str(self.online_count)

    class Meta:
        verbose_name = '在线统计'
        verbose_name_plural = '在线统计'

#玩家领取礼物码记录
class Usegift_Record(models.Model):
    uid =models.CharField('玩家UID',max_length=200,default='',blank=True,null=True)
    server_id=models.CharField('区服ID',max_length=100,default='',blank=True,null=True)
    cdk=models.CharField('CDK',max_length=100,default='',blank=True,null=True)
    create_time=models.DateTimeField('生成时间',auto_now_add=True)
    piciid=models.CharField('批次ID',max_length=100,default='',blank=True,null=True)


    def __str__(self):
        return self.uid

    class Meta:
        verbose_name = '玩家领取礼物码记录'
        verbose_name_plural = '玩家领取礼物码记录'